<?php
/**
 * 《物料》实用类
 *
 * @author 孙晓晔
 * @version $Id: MaterialUtil.class.php 1044 2010-04-02 09:32:24Z sunxy $
 */
require_once(dirname(__FILE__) . '/../AdminUtil.class.php');

class MaterialUtil extends AdminUtil {
	
	public $place = array(
		'-' => '请选择',
		'大模厂' => '大模厂',
		'精密模仓' => '精密模仓',
		'工模仓库' => '工模仓库',
		'塑胶仓库' => '塑胶仓库',
	
		'产品仓库' => '产品仓库',
		'半成品仓' => '半成品仓',
		'产品仓库' => '产品仓库',
		
		'电脑配件' => '电脑配件',		
		'办公用品' => '办公用品',
	
		'其它' => '其它',
	);
	
	public $address = array(
		'-' => '请选择',
		'沙井镇步涌同富裕工业区A-5地块B3栋' => '沙井镇步涌同富裕工业区A-5地块B3栋',
		'清溪镇三中村顺峰路景荣厂' => '清溪镇三中村顺峰路景荣厂',
		'南山区南山大道1088号南园枫叶大厦8C' => '南山区南山大道1088号南园枫叶大厦8C',
	);
	
	public $unit = array(
		'-' => '请选择',
		'件' => '件',
		'套' => '套',
		'块' => '块',
		'个' => '个',	
		'把' => '把',
		'次' => '次',
		'千克' => '千克',
		'公升' => '公升',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/03/03 07:24:42 $';
		$request['revision'] = '$Revision: 1.5 $';
			
		parent::__construct($request);
	}

	/**
	 * 预处理
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('currency_option', $this->currency);
		$controller->assign_by_ref('unit_option', $this->unit);
		
		$controller->assign_by_ref('place_option', $this->place);
		$controller->assign_by_ref('address_option', $this->address);
		
		$this->t = array_merge($this->t, $this->b);
	}
	
	public $b = array(
		'car'=>'请输入车辆编号，然后选择并回车即可。',
		'driver'=>'请输入司机姓名拼音首字母，然后选择并回车即可。如张三，输入zs',
	);

	function _code_exist($code) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $code);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}

		return false;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function delete($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		while (list($k, $id) = each($request)) {
			if (substr($k, 0, 2) <> "c_") {
				continue;
			}

			$fmt = "DELETE FROM %s WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $id);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 * 出库
	 */
	function out_($data) {
		$material = $data['material'];

		$name = addslashes($data['name']);
		$standard = addslashes($data['standard']);
		$amount = $data['amount'];

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s WHERE name = '%s' AND standard = '%s'";
		$sql = sprintf($fmt, Constant::tbl_material_amount, $name, $standard);
		debug($sql);
		$result = $db->query($sql);
		if ($row = $db->fetch_array($result)) {
			$fmt = "UPDATE %s SET amount = amount - %s, modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, Constant::tbl_material_amount, $amount, $row['id']);
		}

		debug($sql);
		$db->query($sql);
		if ($db->affected_rows() > 0) {
			$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, Constant::tbl_wl_record_out, '9', $data['id']);
			$db->query($sql);
		}
	}
	
	function _status($id, $status) {
		$this->_update_status($id, $status);
	}
	
	function _update_status($id, $status) {
		$db = newdb($this->dsn_w);		
		$fmt = "UPDATE %s SET status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $status, $id);
		debug($sql);
		$db->query($sql);		
		if ($db->affected_rows() > 0) {
			return true;
		}
			
		return false;
	}
	
	function _print_content($s) {
		$a = preg_split("/[\\n]+/i", $s);
	
		if (count($a) > 1) {
			return $a[0];
		}
	
		return $s;
	}
	
	function _v1($s) {
		// $s = "230*120*50";
		$a = preg_split("/[\*|x]/i", $s);
		$r = floatval($a[0]) * floatval($a[1]) * floatval($a[2]) * pow(10, -9);
	
		return $r;
	}
	
	function _v2($s) {
		// $s = "230*120";
		$a = preg_split("/[\*|x]/i", $s);
		$b = floatval($a[0]) / 2;
		$r = pi() * pow($b, 2) * floatval($a[1]) * pow(10, -9);
	
		return $r;
	}
	
	function _v($row) {
		// $s = "230*120*50";
		$a = preg_split("/[\*|x]/i", $row['standard']);
		if ($row['type'] == 2) {
			$row['length'] = $a[0];
			$row['width'] = $a[1];
			$row['height'] = $a[2];
			return $row;
		}
	
		if ($row['type'] == 3) {
			$row['diameter'] = $a[0];
			$row['height'] = $a[1];
			return $row;
		}
	
		return $row;
	}
}
